package qingxin.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import qingxin.dao.UserDAO;
import qingxin.dbc.DatabaseConnection;
import qingxin.vo.User;

public class UserDAOImpl implements UserDAO {

	@Override
	public User getByID(int userID) throws Exception {
		User user = null;

		String sql = "select username, avatar, type, third_party_ID from user where uid = ?";
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);
			pstmt.setInt(1, userID);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				user = new User();
				user.setUserID(userID);
				user.setUserName(rs.getString(1));
				user.setAvatar(rs.getString(2));
				user.setType(rs.getInt(3));
				user.setThirdPartyID(rs.getLong(4));
			}
		}
		
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		return user;
	}

	@Override
	public User isExist(int type, long thirdID) throws Exception {
		User user = null;
		
		String sql = "select uid from user where type = ? and third_party_id = ?";
		DatabaseConnection dbc = null;
		try{
			dbc= new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);
			
			pstmt.setInt(1, type);
			pstmt.setLong(2, thirdID);
			
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()){
				user = new User();
				user.setUserID(rs.getInt(1));
			}
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		
		return user;
	}

	@Override
	public void insert(User user) throws Exception {
		String sql = "insert into user (type, third_party_id, username, avatar) values (?,?,?,?)";
		
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			
			pstmt.setInt(1, user.getType());
			pstmt.setLong(2, user.getThirdPartyID());
			pstmt.setString(3, user.getUserName());
			pstmt.setString(4, user.getAvatar());
			
			pstmt.executeUpdate();
			
			ResultSet key = pstmt.getGeneratedKeys();
			if(key.next()){
				user.setUserID(key.getInt(1));
			}
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
		
	}

	@Override
	public void update(User user) throws Exception {
		String sql = "update user set type=?, third_party_id=?, username=?,avatar=? where uid=?";
		DatabaseConnection dbc = null;
		try{
			dbc = new DatabaseConnection();
			PreparedStatement pstmt = dbc.getConnection().prepareStatement(sql);
			
			pstmt.setInt(1, user.getType());
			pstmt.setLong(2, user.getThirdPartyID());
			pstmt.setString(3, user.getUserName());
			pstmt.setString(4, user.getAvatar());
			pstmt.setInt(5, user.getUserID());
			
			pstmt.executeUpdate();
		}
		catch(Exception e){
			throw e;
		}
		finally{
			dbc.close();
		}
	}

}
